package com.atguigu.bigdata.chapter07.state;

import com.atguigu.bigdata.bean.WaterSensor;
import com.atguigu.bigdata.util.AtguiguUtil;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Comparator;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/9/6 15:03
 */
public class Flink05_Key_List {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        // 每隔3秒中做一次checkpoint
        // 这样理解: 每隔3秒,把程序的所有的状态做一次持久化, 程序出了问题重启的时候, 可以从持久化状态中恢复数据
        env.enableCheckpointing(3000);
        
        // 在flatMap算子中使用算子状态
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(
                    data[0],
                    Long.valueOf(data[1]),
                    Integer.valueOf(data[2])
                );
            })
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                
                
                private ListState<Integer> top3VcState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    // 获取监控状态实例
                    top3VcState = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("top3VcState", Integer.class));
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    // 每来一个水位值, 就先存入到列表状态中
                    // 覆盖操作: 用心的集合中的值, 去覆盖状态中的值
                    //                    top3VcState.update();
                    // 添加操作: 把list中的元素添加到状态中
                    //                    top3VcState.addAll(list);
                    // 添加操作: 向状态中添加一个值
                    top3VcState.add(value.getVc());
                    
                    // 状态中所有的值取出, 排序, 取top3
                    Iterable<Integer> it = top3VcState.get();
                    List<Integer> vcList = AtguiguUtil.toList(it);
                    /*vcList.sort(new Comparator<Integer>() {
                        @Override
                        public int compare(Integer o1, Integer o2) {
                            return o2.compareTo(o1);
                        }
                    });*/
//                    vcList.sort((o1, o2) -> o2.compareTo(o1));
                    // 降序
                    vcList.sort(Comparator.reverseOrder());
    
                    // 如果发现长度等于4, 就干掉一个, 保持长度是3
                    if (vcList.size() == 4) {
                        vcList.remove(vcList.size() - 1);
                    }
                    
                    // 用list中值, 去覆盖状态中的值
                    top3VcState.update(vcList);
    
                    out.collect(ctx.getCurrentKey() + " 的top3: " + vcList);
    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
